package context;


// ********************************************************************
// *** CLASS: Debug
// ********************************************************************
/**
 * <pre>
 * Typical usage:
 *
 * 1) To show the debug text screen:
 *       Debug.show(display);
 *
 * 2) Anywhere:
 *       Debug.println(" any text ");
 * </pre>
 */
public class Debug
{   
  
  public static Boolean lock=new Boolean(false); // To synchronize print
  public static String IDENT="";
//  private static String threadID(){return(Thread.currentThread().getName());}
  
  public static void log(String who,String msg) { println(" LOG: "+msg); }
  public static void ERROR(String s) { ERROR("ERROR",s,null); }
//  public static void EXCEPTION(String s,Exception e) { ERROR("EXCEPTION",s+""+e.getMessage(),e); }
  public static void EXCEPTION(String s,Exception e) { ERROR("EXCEPTION",e.toString()+": "+s,e); }
  public static void NOT_IMPLEMENTED(String s) { println("*** NOT IMPLEMENTED: "+s); }
  
  public static void ASSERT(boolean test,String msg)
  { if(!test)
    { synchronized(lock)
      { try { throw new RuntimeException("ASSERT("+msg+") -- FAILED"); }
        catch(Exception e) { e.printStackTrace(); }
      }
      //ERROR("ASSERTION FAILED",msg,null);
    }
  }

  public static void ERROR(String title,String s,Exception e)
  { synchronized(lock)
	{ String msg="*** "+title+": "+s;
      println(msg);
      if(e!=null) e.printStackTrace();
    }
  }

  public static void println(String s)
  { synchronized(lock)
    { try
      { s=s.replace('\r',(char)0);
        s=s.replace('\n',(char)0);
        System.out.println(IDENT+s);
      }
      catch(Exception e)
      { System.out.println("*** EXCEPTION while printing debug info: "+e); }
    }
  }
  
}
